package floappcontest.core.backbeans;

import java.util.ArrayList;
import java.util.List;

import floappcontest.core.beans.TeachingInstitution;
import floappcontest.core.beans.TeachingInstitutionType;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;

/**
 * The TeamBean is a BackBean (JavaBean) related with the 
 * teaching institution events (insert, delete, edit, etc) in the pages.  
 */
public class TeachingInstitutionBean {
	
	private static final String SLASH = "/"; //$NON-NLS-1$

	private static final String DIGIT_SEPARATOR = "-"; //$NON-NLS-1$

	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Control control;
	
	private TeachingInstitution ies;
	
	private TeachingInstitutionType typePublic = TeachingInstitutionType.PUBLIC;
	private TeachingInstitutionType typesPrivate = TeachingInstitutionType.PRIVATE;
	
	private String tiType;
	
	private List<TeachingInstitution> list;
	
	private Integer ddd;
	
	private Integer phoneNumber;
	
	private Integer cnpjA;
	
	private Integer cnpjB;
	
	private Integer cnpjC;
	
	
	/**
	 * 
	 */
	public TeachingInstitutionBean() {
		setControl( new Control( new HibernateDAOFactory() ) );
		setIes( new TeachingInstitution() );
		refreshList();
	}
	
	/**
	 * @return
	 */
	public TeachingInstitution getIes() {
		return this.ies;
	}

	/**
	 * @param ies
	 */
	public void setIes(TeachingInstitution ies) {
		this.ies = ies;
	}
	
	/**
	 * Navigation handling method (Action methods) that insert a teaching institution. 
	 * @return "OK" if the insertion occur without problems, otherwise "FAILED".
	 */
	public String insertTeachingInstitution() {
		try {
			collectFields();
			System.out.println(getIes());
			getControl().insertTeachingInstitution( getIes() );
			setIes( new TeachingInstitution() );
			return OK;
		} catch (FloAppException e) {
			setIes( new TeachingInstitution() );
			return FAILED;
		} finally {
			refreshList();
		}
	}
	
	private void collectFields() {
		TeachingInstitution temp = getIes();
		temp.setType( getTiType().equalsIgnoreCase( Boolean.TRUE.toString() )? TeachingInstitutionType.PUBLIC: TeachingInstitutionType.PRIVATE );
		ArrayList<String> telephones = new ArrayList<String>();
		telephones.add( getDdd() + DIGIT_SEPARATOR + getPhoneNumber() );
		temp.setTelephone( telephones );
		temp.setCnpj( getCnpjA() + SLASH + getCnpjB() + DIGIT_SEPARATOR + getCnpjC() );
	}

	/**
	 * @return
	 */
	public Control getControl() {
		return this.control;
	}

	/**
	 * @param control
	 */
	public void setControl(Control control) {
		this.control = control;
	}
	
	/**
	 * @return
	 */
	public void refreshList(){		
		setList( getControl().listTeachingInstitutions() );		
	}

	public List<TeachingInstitution> getList() {
		return this.list;
	}

	public void setList(List<TeachingInstitution> list) {
		this.list = list;
	}
	
	public String removeTeachingInstitution(){
		try {
			getControl().removeTeachingInstitution( getIes().getName() );
			return new MenuBarRedirection().listIesPage();
		} catch (FloAppException e) {
			e.printStackTrace();
			return FAILED;
		}
	}

	public TeachingInstitutionType getTypePublic() {
		return this.typePublic;
	}

	public void setTypePublic(TeachingInstitutionType typePublic) {
		this.typePublic = typePublic;
	}

	public TeachingInstitutionType getTypesPrivate() {
		return this.typesPrivate;
	}

	public void setTypesPrivate(TeachingInstitutionType typesPrivate) {
		this.typesPrivate = typesPrivate;
	}

	
	public String getTiType() {
		return this.tiType;
	}

	
	public void setTiType( String tiType ) {
		this.tiType = tiType;
	}

	
	public Integer getDdd() {
		return this.ddd;
	}

	
	public Integer getPhoneNumber() {
		return this.phoneNumber;
	}

	
	public Integer getCnpjA() {
		return this.cnpjA;
	}

	
	public Integer getCnpjB() {
		return this.cnpjB;
	}

	
	public Integer getCnpjC() {
		return this.cnpjC;
	}

	
	public void setDdd( Integer ddd ) {
		this.ddd = ddd;
	}

	
	public void setPhoneNumber( Integer phoneNumber ) {
		this.phoneNumber = phoneNumber;
	}

	
	public void setCnpjA( Integer cnpjA ) {
		this.cnpjA = cnpjA;
	}

	
	public void setCnpjB( Integer cnpjB ) {
		this.cnpjB = cnpjB;
	}

	
	public void setCnpjC( Integer cnpjC ) {
		this.cnpjC = cnpjC;
	}
}